<?php

/**
 * 扫描现有存量淘宝商品数据有场景图的 检查图片链接是否已失效，如果失效则把场景图置空
 */

ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
error_reporting(E_ERROR | E_PARSE);

require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\Wiki as mWiki;
use Biz\pockets\excel\DownExcel;
use Biz\tools\Mail;
use Doraemon\tools\Log;

const EMAIL_MAP = [
    'renzhifan@haohaozhu.com',
];
//$url = 'https://img.alicdn.com/i1/692996526/O1CN015pnwoL1y4xOaUDOOi_!!692996526.jpg'; //失效图片链接
//$url = 'https://img.alicdn.com/bao/uploaded/O1CN01HuACGE1aXJ9xJrDWv_!!6000000003339-0-yinhe.jpg';//正常图片链接
//var_dump(getHttpCode($url));
//die;
try {
    $programStartTime = time();
    //查询符合条件的商品
    $where = [
        'status[!]' => mWiki::STATUS_DEL,
        'channel' => mWiki::CHANNEL_TAOBAO,
        'scene_img[!]' => '',
        "ORDER" => ['id' => 'DESC'],
    ];
    $maxWikiId = mWiki::get('id', $where) ?? 0;
    $pageSize = 4000;

    $page = ceil($maxWikiId / $pageSize);
    $msgAll = '';
    $i = 0;
    $list = [];
    while ($i < $page) {
        $start = $i * $pageSize;
        $end = ($i + 1) * $pageSize;
        unset($where['ORDER']);
        $wikiInfos = mWiki::select(['id', 'cover_img', 'white_image', 'scene_img'],
            array_merge($where, ["id[<>]" => [$start, $end]]));
        $j = 0;
        foreach ($wikiInfos as $wiki) {
            //如果场景图连接访问失效 则把场景图置为空
            if (getHttpCode($wiki['scene_img']) == 404) {
                log::info('wikiId:'.$wiki['id'], ['scene_img' => $wiki['scene_img']]);
                echo $wiki['scene_img'].PHP_EOL;
                mWiki::update(['scene_img' => ''], ['id' => $wiki['id']]);
                $j++;
                $list[] = ['id' => $wiki['id'], 'scene_img' => $wiki['scene_img']];
            }
        }
        $i++;
        $msg = '第'.$i."批数据更新完成;共修改".$j.'条数据;'.PHP_EOL;
        echo $msg;
        $msgAll .= $msg;
    }
    $filePath = export($list);
    $pageTitle = 'FixWikiSceneImgNotFound';
    Mail::send(EMAIL_MAP, $pageTitle, '已失效的场景图链接记录-见附件', $filePath);
    echo '发送邮件成功'.PHP_EOL;
    $programEndTime = time();
    echo '共消耗时间'.($programEndTime - $programStartTime).PHP_EOL;
    Log::info('FixWikiSceneImgNotFound.php执行成功', ['$msgAll' => $msgAll]);
} catch (Exception $exception) {
    Log::error('FixWikiSceneImgNotFound.php执行失败', $exception);
}

function getHttpCode($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 1);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //把数据存成字符串

    if (!curl_exec($curl)) {  //开始执行
        $code = 500;
    } else {
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    }

    curl_close($curl); //用完关掉

    return $code;
}

function export($list)
{
    $header = [
        'wiki_id',
        'scene_img',
    ];
    $excel_obj = new DownExcel();
    $excel_obj->saveType = 1;
    $excel_obj->setFileName('FixWikiSceneImgNotFound'.date('Y-m-d-H-i-s'));
    $excel_obj->setSheetTitle('FixWikiSceneImgNotFound');

    // 设置列宽
    $widths = [
        'A' => 15,
        'B' => 100,
    ];

    $excel_obj->setWidth($widths);
    $excel_obj->setDefaultStyleAlignment();
    $excel_obj->setTextFormat('A');
    $excel_obj->setTextFormat('B');

    return ($excel_obj->create($list, $header));
}